<?php
	$s_error = "";
	$s_msg = "";
	$arr_types = Array("image/png", "image/gif", "image/jpeg","image/pjpeg","image/x-png");
	
	$o_file_element = 'fileToUpload';
	if(!empty($_FILES[$o_file_element]['error']))
	{
		switch($_FILES[$o_file_element]['error'])
		{

			case '1':
				$s_error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
				break;
			case '2':
				$s_error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
				break;
			case '3':
				$s_error = 'The uploaded file was only partially uploaded';
				break;
			case '4':
				$s_error = 'No file was uploaded.';
				break;

			case '6':
				$s_error = 'Missing a temporary folder';
				break;
			case '7':
				$s_error = 'Failed to write file to disk';
				break;
			case '8':
				$s_error = 'File upload stopped by extension';
				break;
			case '999':
			default:
				$s_error = 'No error code avaiable';
		}
	}elseif(empty($_FILES[$o_file_element]['tmp_name']) || $_FILES[$o_file_element]['tmp_name'] == 'none')
	{
		$s_error = 'No file was uploaded..';
	}else 
	{
		
		if (in_array($_FILES[$o_file_element]["type"], $arr_types)) {
			$s_msg .="<img id=\"img_avatar\" alt=\"khong thay\" src=\"image_account/".$_FILES[$o_file_element]["name"]."\"/>";
			//move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],"image_account/".$_FILES["fileToUpload"]["name"]);
			
			$source = file_get_contents($_FILES[$o_file_element]["tmp_name"]);
			$destination = "./image_account/".$_FILES[$o_file_element]["name"];	
			imageresize($source, $destination, 144, 108, true, 80);
			
			//for security reason, we force to remove all uploaded file
			@unlink($_FILES[$o_file_element]);
		}else {
			$s_error = "Định dạng tập tin không được hỗ trợ, hãy chọn tập tin có định dạng ". implode(", ", $arr_types)."-".$_FILES[$o_file_element]["type"];
		}
	}
	echo "{";
	echo	"error: '" . $s_error . "',\n";
	echo	"msg: '" . $s_msg . "'\n";
	echo "}";
	
	function imageresize($source, $destination, $width = 0, $height = 0, $crop = false, $quality = 80) {
		$quality = $quality ? $quality : 80;
		$image = imagecreatefromstring($source);
		if ($image) {
			// Get dimensions
			$w = imagesx($image);
			$h = imagesy($image);
			if (($width && $w > $width) || ($height && $h > $height)) {
				$ratio = $w / $h;
				if (($ratio >= 1 || $height == 0) && $width && !$crop) {
					$new_height = $width / $ratio;
					$new_width = $width;
				} elseif ($crop && $ratio <= ($width / $height)) {
					$new_height = $width / $ratio;
					$new_width = $width;
				} else {
					$new_width = $height * $ratio;
					$new_height = $height;
				}
			} else {
				$new_width = $w;
				$new_height = $h;
			}
			$x_mid = $new_width * .5;  //horizontal middle
			$y_mid = $new_height * .5; //vertical middle
			// Resample
			error_log('height: '.$new_height.' - width: '.$new_width);
			$new = imagecreatetruecolor(round($new_width), round($new_height));
			imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $w, $h);
			// Crop
			if ($crop) {
				$crop = imagecreatetruecolor($width ? $width : $new_width, $height ? $height : $new_height);
				imagecopyresampled($crop, $new, 0, 0, ($x_mid - ($width * .5)), 0, $width, $height, $width, $height);
				//($y_mid - ($height * .5))
			}
			// Output
			// Enable interlancing [for progressive JPEG]
			imageinterlace($crop ? $crop : $new, true);
	
			$dext = strtolower(pathinfo($destination, PATHINFO_EXTENSION));
			if ($dext == '') {
				$dext = $ext;
				$destination .= '.' . $ext;
			}
			switch ($dext) {
				case 'jpeg':
				case 'jpg':
					imagejpeg($crop ? $crop : $new, $destination, $quality);
					break;
				case 'png':
					$pngQuality = ($quality - 100) / 11.111111;
					$pngQuality = round(abs($pngQuality));
					imagepng($crop ? $crop : $new, $destination, $pngQuality);
					break;
				case 'gif':
					imagegif($crop ? $crop : $new, $destination);
					break;
			}
			@imagedestroy($image);
			@imagedestroy($new);
			@imagedestroy($crop);
		}
	}
?>